import 'dart:math';

import 'package:flutter/material.dart';

class BuildingList extends StatefulWidget {
  const BuildingList({super.key, required this.point});

  final String point;

  @override
  State<BuildingList> createState() => _RoomListState();
}

class _RoomListState extends State<BuildingList> {
  Map data = {
    "point": "", // 记录小区信息
    "size": 0, // 随机产生楼栋的数量
    "type": "", // 小区的楼栋名称(size>4 单元, size<=4 号楼)
  };
  @override
  void initState() {
    super.initState();
    // setState(() {
    //   data['point'] = widget.point;
    // });
    fakeData();
  }
  //随机产出3-8条数据
  //如果数据大于5条，则产生单元，小于5条，产生楼栋
  void fakeData(){
    Random random = Random();
    int size = random.nextInt(6) + 3;
    String type = size < 5 ? '号楼' : '单元';
    setState(() {
      data['size'] = size;
      data['type'] = type;
      data['point'] = widget.point;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('选择楼栋'),
      ),
      body: ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          return GestureDetector(
            onTap: (){
              Navigator.pushNamed(context, '/room_list',arguments: {
                'point':data['point'],
                'building':'${index+1}${data['type']}'
              });
            },
            child: Container(
            color: Colors.white,
            padding: const EdgeInsets.all(10),
            child: Row(
              children: [
                Expanded(child: Text('${data['point']}${index+1}${data['type']}')),
                Row(
                  children: [
                    Icon(Icons.arrow_forward_ios,
                        size: 16, color: Colors.black),
                  ],
                )
              ],
            ),
          ),
          );
        },
        itemCount: data['size'],
      ),
    );
  }
}
